package com.orangelife.service.propFeeMeterRecordImportLog.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import com.github.pagehelper.PageHelper;
import com.orangelife.common.MybaitsGenericDaoSubLogic;
import com.orangelife.exception.AppBizException;
import com.orangelife.service.propFeeMeterRecordImportLog.IPropFeeMeterRecordImportLogService;
import com.orangelife.service.propFeeMeterRecordImportLog.model.PropFeeMeterRecordImportLogDto;
import com.orangelife.setting.constant.CodeConst;
import com.orangelife.setting.constant.InfoConst;
import com.orangelife.setting.constant.TypeConst;
import com.orangelife.utils.IdUtil;
import com.orangelife.utils.StringUtils;

/*****************************************************************
 * Copyright(C) ORANGELIFE Corporation CodeConst.BIS_CODE_68001015. All Rights
 * Reserved.
 *****************************************************************/
/*
 * ============================================================== 功能名
 * PropFeeMeterRecordImportLog类的 service层的增删改查
 * 
 * @author @author zhr
 * 
 * @version 1.1.0
 * 
 * @date 2016-08-31 17:44:10
 * ==============================================================
 */
@Service("propFeeMeterRecordImportLogService")
public class PropFeeMeterRecordImportLogServiceImpl extends
		MybaitsGenericDaoSubLogic implements
		IPropFeeMeterRecordImportLogService {
	private static Logger logger = Logger
			.getLogger(PropFeeMeterRecordImportLogServiceImpl.class);

	public static Logger getLogger() {
		return logger;
	}

	public static void setLogger(Logger logger) {
		PropFeeMeterRecordImportLogServiceImpl.logger = logger;
	}

	@Override
	@Transactional(rollbackFor = { AppBizException.class, Exception.class,
			RuntimeException.class })
	public int addPropFeeMeterRecordImportLog(
			PropFeeMeterRecordImportLogDto propFeeMeterRecordImportLog)
			throws AppBizException {

		Assert.notNull(propFeeMeterRecordImportLog);
		/************************************************
		 * 判断输入参数的长度以及是否能为空
		 ************************************************/

		if (!StringUtils.isBlank(propFeeMeterRecordImportLog.getFileName())) {
			if (propFeeMeterRecordImportLog.getFileName().length() > 256) {
				throw new AppBizException(CodeConst.SYS_CODE_1002, "文件名称长度超出");
			}
		}

		if (!StringUtils.isBlank(propFeeMeterRecordImportLog.getStatus())) {
			if (propFeeMeterRecordImportLog.getStatus().length() > 1) {
				throw new AppBizException(CodeConst.SYS_CODE_1002,
						"1导入中,2上传成功，3上传失败长度超出");
			}
		}

		if (!StringUtils.isBlank(propFeeMeterRecordImportLog.getImportInfo())) {
			if (propFeeMeterRecordImportLog.getImportInfo().length() > 256) {
				throw new AppBizException(CodeConst.SYS_CODE_1002, "上传信息长度超出");
			}
		}

		if (!StringUtils.isBlank(propFeeMeterRecordImportLog.getLink())) {
			if (propFeeMeterRecordImportLog.getLink().length() > 256) {
				throw new AppBizException(CodeConst.SYS_CODE_1002,
						"错误报告日志链接长度超出");
			}
		}

		if (!StringUtils.isBlank(propFeeMeterRecordImportLog.getRemark())) {
			if (propFeeMeterRecordImportLog.getRemark().length() > 256) {
				throw new AppBizException(CodeConst.SYS_CODE_1002, "长度超出");
			}
		}

		propFeeMeterRecordImportLog.setImportLogID(IdUtil.getuuId());
		propFeeMeterRecordImportLog.setItemStat(TypeConst.FLAG_ON);
		propFeeMeterRecordImportLog.setTscreate(new Date());
		propFeeMeterRecordImportLog.setTsedit(new Date());
		return this.getSqlSession().insert(
				"PropFeeMeterRecordImportLogMapper.insertSelective",
				propFeeMeterRecordImportLog);
	}

	@Override
	@Transactional(rollbackFor = { AppBizException.class, Exception.class,
			RuntimeException.class })
	public int editPropFeeMeterRecordImportLog(
			PropFeeMeterRecordImportLogDto propFeeMeterRecordImportLog)
			throws AppBizException {

		Assert.notNull(propFeeMeterRecordImportLog);
		/************************************************
		 * 判断输入参数的长度以及是否能为空
		 ************************************************/
		if (StringUtils.isBlank(propFeeMeterRecordImportLog.getImportLogID())) {
			throw new AppBizException(CodeConst.SYS_CODE_1001, "ID不可为空");
		}
		PropFeeMeterRecordImportLogDto oldPropFeeMeterRecordImportLog = this
				.findPropFeeMeterRecordImportLogByID(propFeeMeterRecordImportLog
						.getImportLogID());

		if (!StringUtils.isBlank(propFeeMeterRecordImportLog.getFileName())) {
			if (propFeeMeterRecordImportLog.getFileName().length() > 256) {
				throw new AppBizException(CodeConst.SYS_CODE_1002, "文件名称长度超出");
			}
		}

		if (!StringUtils.isBlank(propFeeMeterRecordImportLog.getStatus())) {
			if (propFeeMeterRecordImportLog.getStatus().length() > 1) {
				throw new AppBizException(CodeConst.SYS_CODE_1002,
						"1导入中,2上传成功，3上传失败长度超出");
			}
		}

		if (!StringUtils.isBlank(propFeeMeterRecordImportLog.getImportInfo())) {
			if (propFeeMeterRecordImportLog.getImportInfo().length() > 256) {
				throw new AppBizException(CodeConst.SYS_CODE_1002, "上传信息长度超出");
			}
		}

		if (!StringUtils.isBlank(propFeeMeterRecordImportLog.getLink())) {
			if (propFeeMeterRecordImportLog.getLink().length() > 256) {
				throw new AppBizException(CodeConst.SYS_CODE_1002,
						"错误报告日志链接长度超出");
			}
		}

		if (!StringUtils.isBlank(propFeeMeterRecordImportLog.getRemark())) {
			if (propFeeMeterRecordImportLog.getRemark().length() > 256) {
				throw new AppBizException(CodeConst.SYS_CODE_1002, "长度超出");
			}
		}

		if (oldPropFeeMeterRecordImportLog == null) {
			throw new AppBizException(CodeConst.BIS_CODE_68001,
					InfoConst.BIS_INFO_68001);
		}
		propFeeMeterRecordImportLog.setTsedit(new Date());
		return this
				.getSqlSession()
				.update("PropFeeMeterRecordImportLogMapper.updateByPrimaryKeySelective",
						propFeeMeterRecordImportLog);
	}

	@Override
	public PropFeeMeterRecordImportLogDto findPropFeeMeterRecordImportLogByID(
			String propFeeMeterRecordImportLogID) throws AppBizException {
		if (StringUtils.isBlank(propFeeMeterRecordImportLogID)) {

			throw new AppBizException(CodeConst.SYS_CODE_1001, "ID不可为空");
		}

		/************************************************
		 * 判断实体是否存在
		 ************************************************/
		PropFeeMeterRecordImportLogDto oldPropFeeMeterRecordImportLog = this
				.getSqlSession()
				.selectOne(
						"PropFeeMeterRecordImportLogSearchMapper.selectByPrimaryKey",
						propFeeMeterRecordImportLogID);
		if (oldPropFeeMeterRecordImportLog == null) {
			throw new AppBizException(CodeConst.BIS_CODE_68001,
					InfoConst.BIS_INFO_68001);
		}

		if (TypeConst.FLAG_OFF.equals(oldPropFeeMeterRecordImportLog
				.getItemStat())) {

			throw new AppBizException(CodeConst.BIS_CODE_68001,
					InfoConst.BIS_INFO_68001);
		}
		return oldPropFeeMeterRecordImportLog;
	}

	@Override
	@Transactional(rollbackFor = { AppBizException.class, Exception.class,
			RuntimeException.class })
	public int deletePropFeeMeterRecordImportLogByLogic(
			String propFeeMeterRecordImportLogID) throws AppBizException {

		if (StringUtils.isBlank(propFeeMeterRecordImportLogID)) {
			throw new AppBizException(CodeConst.SYS_CODE_1001, "ID不可为空");
		}
		/************************************************
		 * 判断实体是否存在
		 ************************************************/
		PropFeeMeterRecordImportLogDto oldPropFeeMeterRecordImportLog = this
				.findPropFeeMeterRecordImportLogByID(propFeeMeterRecordImportLogID);

		oldPropFeeMeterRecordImportLog.setItemStat(TypeConst.FLAG_OFF);
		oldPropFeeMeterRecordImportLog.setTsedit(new Date());
		return this
				.getSqlSession()
				.update("PropFeeMeterRecordImportLogMapper.updateByPrimaryKeySelective",
						oldPropFeeMeterRecordImportLog);
	}

	@Override
	public List<Map<String, Object>> findPropFeeMeterRecordImportLogByPage(
			String commID, String tsCreate, String search, int pageNo,
			int pageSize) throws AppBizException {

		/************************************************
		 * 设置输入参数
		 ************************************************/
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("commID", commID);
		map.put("tsCreate", tsCreate);
		map.put("search", search);
		PageHelper.startPage(pageNo, pageSize);
		List<Map<String, Object>> list = this
				.getSqlSession()
				.selectList(
						"PropFeeMeterRecordImportLogSearchMapper.findAllPropFeeMeterRecordImportLog",
						map);
		return list;
	}

	@Override
	public List<PropFeeMeterRecordImportLogDto> getPropFeeMeterRecordImportLogByPage(
			String tsCreate, String search, int pageNo, int pageSize)
			throws AppBizException {
		/************************************************
		 * 设置输入参数
		 ************************************************/
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("tsCreate", tsCreate);
		map.put("search", search);
		PageHelper.startPage(pageNo, pageSize);
		List<PropFeeMeterRecordImportLogDto> list = this
				.getSqlSession()
				.selectList(
						"PropFeeMeterRecordImportLogSearchMapper.getAllPropFeeMeterRecordImportLogDto",
						map);
		return list;
	}

	@Override
	public List<Map<String, Object>> selectPropFeeMeterRecordImportLogByRelevantID(
			String importLogID, String status, String commID)
			throws AppBizException {
		/************************************************
		 * 设置输入参数
		 ************************************************/
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("importLogID", importLogID);
		map.put("status", status);
		map.put("commID", commID);

		List<Map<String, Object>> list = this
				.getSqlSession()
				.selectList(
						"PropFeeMeterRecordImportLogSearchMapper.selectPropFeeMeterRecordImportLogDtoyRelevantID",
						map);
		return list;
	}

	@Override
	public List<PropFeeMeterRecordImportLogDto> findPropFeeMeterRecordImportLogByRelevantID(
			String importLogID, String status, String commID)
			throws AppBizException {
		/************************************************
		 * 设置输入参数
		 ************************************************/
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("importLogID", importLogID);
		map.put("status", status);
		map.put("commID", commID);

		List<PropFeeMeterRecordImportLogDto> list = this
				.getSqlSession()
				.selectList(
						"PropFeeMeterRecordImportLogSearchMapper.findPropFeeMeterRecordImportLogDtoByRelevantID",
						map);
		return list;
	}

	@Override
	public List<PropFeeMeterRecordImportLogDto> bauthfindPropFeeMeterRecordImportLog(
			List<String> propFeeMeterRecordImportLogIDs) throws AppBizException {
		/************************************************
		 * 设置输入参数
		 ************************************************/
		List<PropFeeMeterRecordImportLogDto> list = this
				.getSqlSession()
				.selectList(
						"PropFeeMeterRecordImportLogSearchMapper.batchFindPropFeeMeterRecordImportLog",
						propFeeMeterRecordImportLogIDs);
		return list;
	}

	@Override
	public List<Map<String, Object>> bauthfindPropFeeMeterRecordImportLogByMap(
			Map<String, Object> propFeeMeterRecordImportLog)
			throws AppBizException {
		/************************************************
		 * 设置输入参数
		 ************************************************/
		List<Map<String, Object>> list = this
				.getSqlSession()
				.selectList(
						"PropFeeMeterRecordImportLogSearchMapper.batchFindPropFeeMeterRecordImportLogByMap",
						propFeeMeterRecordImportLog);
		return list;
	}

	@Override
	public void clearCache() throws AppBizException {
		System.out.println("清除缓存成功");
	}

}